/**
 * @class Ext.form.TextField
 * 增加中文长度的校验规则
 */
Ext.apply(Ext.form.TextField.prototype, {
    /**
     * @cfg {Boolean} validateCnLength True表示验证中文混合长度, 一个汉字计算两个字节 (defaults to false), 当执行{@link #validator}函数时, 字符串长度校验逻辑:
     * <pre><code>
if (this.validateCnLength ? value.cnLength() < this.minLength : value.length < this.minLength) {
    errors.push(String.format(this.minLengthText, this.minLength));
}

if (this.validateCnLength ? value.cnLength() > this.maxLength : value.length > this.maxLength) {
    errors.push(String.format(this.maxLengthText, this.maxLength));
}
     * </code></pre>
     */
    validateCnLength: false,
    
    /**
     * @cfg {String} trimOnblur 当焦点移除时去除{@link #value}两侧空格 (defaults to 'undo')
     * <ul>
     * <li>undo : 什么也不做 <b>Default</b></li>
     * <li>trim : 去除两边空格</li>
     * <li>left : 去除左边空格</li>
     * <li>right : 去除右边空格</li>
     * </ul>
     */
    trimOnblur: 'undo',
    
    // private
    originalInitEvents: Ext.form.TextField.prototype.initEvents,

    // private
    initEvents: function() {
        this.originalInitEvents.apply(this, arguments);
        if (this.trimOnblur != 'undo') {
            this.mon(this.el, 'blur', this.trimValue, this/*, {buffer: this.validationDelay}*/);
        }
    },
    
    // private
    trimValue: function() {
        var val = this.getValue();
        if (this.trimOnblur == 'trim') {
            val = val.trim();
        } else if (this.trimOnblur == 'left') {
            val = val.lTrim();
        } else if (this.trimOnblur == 'right') {
            val = val.rTrim();
        }
        this.setValue(val);
    },

    /**
     * 获取{@link #getValue}返回值长度, 当{@link #validateCnLength}==true时, 返回中文混合长度, 一个汉字计算两个字符
     * @return {Number} 输入值长度
     */
    getLength: function() {
        var v = this.getValue();
        return this.validateCnLength ? v.cnLength() : v.length;
    },

    getErrors: function(value) {
        var errors = Ext.form.TextField.superclass.getErrors.apply(this, arguments);

        value = value || this.processValue(this.getRawValue());

        if (Ext.isFunction(this.validator)) {
            var msg = this.validator(value);
            if (msg !== true) {
                errors.push(msg);
            }
        }

        if (value.length < 1 || value === this.emptyText) {
            if (this.allowBlank) {
                // if value is blank and allowBlank is true, there cannot be any additional errors
                return errors;
            } else {
                errors.push(this.blankText);
            }
        }

        if (!this.allowBlank && (value.length < 1 || value === this.emptyText)) { // if it's blank
            errors.push(this.blankText);
        }

        if (this.validateCnLength ? value.cnLength() < this.minLength : value.length < this.minLength) {
            errors.push(String.format(this.minLengthText, this.minLength));
        }

        if (this.validateCnLength ? value.cnLength() > this.maxLength : value.length > this.maxLength) {
            errors.push(String.format(this.maxLengthText, this.maxLength));
        }

        if (this.vtype) {
            var vt = Ext.form.VTypes;
            if (!vt[this.vtype](value, this)) {
                errors.push(this.vtypeText || vt[this.vtype + 'Text']);
            }
        }

        if (this.regex && !this.regex.test(value)) {
            errors.push(this.regexText);
        }

        return errors;
    }
});
